/*
 * Copyright 2010 Aekold Helbrass
 * 
 * This file is part of RosterComposer.
 * 
 * RosterComposer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RosterComposer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RosterComposer.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.heresylabs.rostercomposer.formatter;

import java.util.List;
import org.heresylabs.rostercomposer.roster.RosterEntry;

/**
 *
 * @author Aekold Helbrass <Helbrass@gmail.com>
 */
public class CompactFormatter extends AbstractFormatter {

    public CompactFormatter(List<RosterEntry> roster) {
        super(roster);
    }

    @Override
    protected void printEntry(RosterEntry entry) {
        // cost:
        printPrice(entry.getCost());
        // divider:
        writer.print(" - ");
        // unit number and name:
        {
            int n = entry.getNumber();
            if (n > 1) {
                writer.print(n);
                writer.print('*');
                writer.print(' ');
            }
            writer.print(entry.getUnit().getShortName());
        }

        // options:
        {
            // we should open '(' only if there are options:
            List<OptionDescription> descriptions = entryOptions(entry);
            if (descriptions != null && descriptions.size() > 0) {

                writer.print(" (");

                int max = descriptions.size() - 1;
                for (int i = 0; i < descriptions.size(); i++) {
                    OptionDescription desc = descriptions.get(i);
                    int n = desc.getCount();
                    if (n > 1) {
                        writer.print(n);
                        writer.print('*');
                    }
                    writer.print(desc.getShortName());
                    if (i != max) {
                        writer.print("; ");
                    }
                }

                writer.print(')');
            }
        }
        writer.println();
    }

    protected void printPrice(int price) {
        String p = String.valueOf(price);
        if (p.length() < mostExpensiveLength) {
            for (int i = p.length(); i < mostExpensiveLength; i++) {
                writer.append(' ');
            }
        }
        writer.print(p);
    }

}
